<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--空状态时的占位提示-->
      <template slot="empty">
        <el-empty>
          <el-button type="primary" @click="resetQuery">刷新试试</el-button>
        </el-empty>
      </template>
      <el-form-item label="要点名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入要点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域" prop="region">
        <el-select v-model="queryParams.region" placeholder="请输入区域" clearable @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in dict.type.showcase_region_1580"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="钢种" prop="steelGrade">
        <el-input
          v-model="queryParams.steelGrade"
          placeholder="请输入钢种"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate({ buttonId: 'multpass' })"
          v-hasPermi="['showcase_1580:technologyExamineApprove1580:edit']"
        >批量通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate({ buttonId: 'multreject' })"
          v-hasPermi="['showcase_1580:technologyExamineApprove1580:edit']"
        >批量驳回</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="technologyExamineApproveList" @selection-change="handleSelectionChange" stripe border highlight-current-row height="650">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="50" align="center">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="要点名称" align="center" prop="name" >
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.name)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.name">
              {{ formatDisplayText(scope.row.name) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="区域" align="center" prop="regionName">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.regionName)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.regionName">
              {{ formatDisplayText(scope.row.regionName) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderNo" width="160">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.orderNo)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.orderNo">
              {{ formatDisplayText(scope.row.orderNo) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="钢种" align="center" prop="steelGrade" width="160">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.steelGrade)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.steelGrade">
              {{ formatDisplayText(scope.row.steelGrade) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="宽下限" align="center" prop="widthLow" width="60"/>
      <el-table-column label="宽上限" align="center" prop="widthHigh" width="60"/>
      <el-table-column label="厚下限" align="center" prop="thicknessLow" width="60"/>
      <el-table-column label="厚上线" align="center" prop="thicknessHigh" width="60"/>
      <el-table-column label="工艺要点" align="center" prop="processS">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.processS)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.processS">
              {{ formatDisplayText(scope.row.processS) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.showcase_examine_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="endDate" width="180" sortable>
        <template slot-scope="scope">
          <span :style="changeColorByData(scope.row.endDate)">{{ scope.row.endDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请日期" align="center" prop="applyDate" width="180" sortable/>
      <el-table-column label="申请人" align="center" prop="applyPerson" sortable/>
      <el-table-column label="审核日期" align="center" prop="examineDate" width="180" sortable/>
      <el-table-column label="审核人" align="center" prop="examinePerson" sortable/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="130">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            plain
            icon="el-icon-edit"
            @click="handleUpdate({ row: scope.row, buttonId: 'singlepass' })"
            v-hasPermi="['showcase_2050:technologyExamine:edit']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审批工艺要点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="540px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="要点名称" prop="name">
          <el-input v-model="form.name" :disabled="true" />
        </el-form-item>
        <el-form-item label="有效期" prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择有效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请人">
          <el-col :span="6">
            <el-form-item prop="applyPerson">
              <el-input v-model="form.applyPerson" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2"></el-col>
          <el-col :span="16">
            <el-form-item label="申请日期" prop="applyDate">
              <el-date-picker clearable
                              v-model="form.applyDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              :disabled="true"
                              placeholder="请选择申请日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="审核人">
          <el-col :span="6">
            <el-form-item prop="examinePerson">
              <el-input v-model="form.examinePerson" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2"></el-col>
          <el-col :span="16">
            <el-form-item label="审核日期" prop="examineDate">
              <el-date-picker clearable
                              v-model="form.examineDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              :disabled="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="审核状态" prop="state">
          <el-radio-group v-model="form.state" fill='#F56C6C'>
            <el-radio label="1" size="medium" class="el-radio-pass">通过</el-radio>
            <el-radio label="2" size="medium" class="el-radio-reject">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listtechnologyExamineApprove,
  gettechnologyExamineApprove,
  updatetechnologyExamineApprove,
} from '@/api/showcase/showcase_1580/technologyExamineApprove1580'
import moment from 'moment';

export default {
  name: "technologyExamineApprove",
  dicts: ['showcase_examine_state','showcase_region_1580'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺要点审批表格数据
      technologyExamineApproveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: null,
        region: null,
        regionName: null,
        orderNo: null,
        steelGrade: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        state: [
          { required: true, message: '请选择是否通过该审核', trigger: 'change' }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工艺要点审批列表 */
    getList() {
      this.loading = true;
      listtechnologyExamineApprove(this.queryParams).then(response => {
        this.technologyExamineApproveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        region: null,
        regionName: null,
        orderNo: null,
        steelGrade: null,
        widthLow: null,
        widthHigh: null,
        thicknessLow: null,
        thicknessHigh: null,
        processS: null,
        state: null,
        endDate: null,
        applyDate: null,
        applyPerson: null,
        examineDate: null,
        examinePerson: null,
        techId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(data) {
      this.reset();
      if (data.buttonId === 'singlepass') { //单个审核
        const id = data.row.id;
        gettechnologyExamineApprove(id).then(response => {
          this.form = response.data;
          this.form.examinePerson = sessionStorage.getItem("currentUser");
          this.form.examineDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
          this.open = true;
          this.title = "审批该工艺要点";
        });
      } else { //批量审核
        const id = this.ids;
        const self = this; // 保存 this 引用
        const text = data.buttonId === 'multpass' ? "通过" : "驳回";
        this.$modal.confirm('该操作会将选中的【' + id.length + '】条申请全部【'+ text +'】，是否确认？').then(function() {
          id.forEach((item) => {
            gettechnologyExamineApprove(item).then(response => {
              self.form = response.data;
              self.form.examinePerson = sessionStorage.getItem("currentUser");
              self.form.examineDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
              self.form.state = data.buttonId === 'multpass' ? '1' : '2';
              //
              updatetechnologyExamineApprove(self.form).then(response => {
                self.$modal.msgSuccess("审核成功");
                self.getList();
              });
            })
          })
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("审核成功");
        }).catch(() => {});
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.state === '0') {
            this.$modal.msgWarning('请选择是否通过该审核');
          } else if (this.form.id != null) {
            updatetechnologyExamineApprove(this.form).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    // 鼠标悬停显示格式化文字
    formatDisplayText(text) {
      if (text && text.length > 30) {
        return text.slice(0, 30) + '...';
      }
      return text;
    },
    formatContent(text) {
      return text ? text.replace(/\n/g, '<br>') : '';
    },

    //根据时间改变文字颜色
    changeColorByData(endDate) {
      const now = new Date();
      const validityEndDate = new Date(endDate);

      if (validityEndDate < now) {
        return 'color: #F56C6C';
      } else if (validityEndDate - now <= 24 * 60 * 60 * 1000) {
        return 'color: #E6A23C';
      }
      return ''
    },

  }
};
</script>
<style scoped>
.cell-content { /*表格内文字防溢出 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 选中后的字体颜色 */
/* 通过的样式 */
.el-radio-pass /deep/ .el-radio__input.is-checked + .el-radio__label {
  color: #67C23A !important; /* 选中时文字颜色 */
}

.el-radio-pass /deep/ .el-radio__input.is-checked .el-radio__inner {
  background-color: #67C23A !important; /* 选中时背景颜色 */
  border-color: #67C23A !important; /* 选中时边框颜色 */
}

/* 不通过的样式 */
.el-radio-reject /deep/ .el-radio__input.is-checked + .el-radio__label {
  color: #F56C6C !important; /* 选中时文字颜色 */
}

.el-radio-reject /deep/ .el-radio__input.is-checked .el-radio__inner {
  background-color: #F56C6C !important; /* 选中时背景颜色 */
  border-color: #F56C6C !important; /* 选中时边框颜色 */
}
</style>
